package com.example.braden.answergame.common.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.braden.answergame.R;
import com.example.braden.answergame.common.LogUtils;
import com.example.braden.answergame.common.utils.CommonUtil;
import com.example.braden.answergame.common.utils.DensityUtil;
import com.example.braden.answergame.component.CardDisplayManager;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by braden on 2018/3/5.
 */

public class BubblePopupWindow extends PopupWindow {

    private Context mContext;
    private View popView;
    private Disposable disposable;
    private TextView tvCount;
    private CardDisplayManager cardDisplayManager;

    public BubblePopupWindow(Context context, CardDisplayManager cardDisplayManager) {
        this.mContext = context;
        this.cardDisplayManager = cardDisplayManager;
        initView();
    }

    public BubblePopupWindow(Context context) {
        this.mContext = context;
        initView();
    }

    private void initView() {
        popView = LayoutInflater.from(mContext).inflate(R.layout.bubble_view, null);
        tvCount = popView.findViewById(R.id.tv_count_down);
        this.setContentView(popView);// 设置View
        this.setWidth(RelativeLayout.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的宽
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(false);// 设置弹出窗口可
        this.setAnimationStyle(R.style.popupAnimation);// 设置动画
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        this.setOutsideTouchable(false);

        popView.setOnTouchListener((view, motionEvent) -> {
            //否则default，往下dispatch事件:关掉popupWindow，
            return false;
        });
    }

    public void cancle() {
        this.dismiss();
        if (disposable != null) {
            disposable.dispose();
        }
    }

    public void showYanshiPopup(View view) {
        setPopup(view);
        tvCount.setText("点击加时5秒");
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(4)
                .map(aLong -> 3 - aLong)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {
                    if (aLong == 0) {
                        this.dismiss();
                    }
                })
                .subscribe();
    }

    public void showPopup(View view) {
        setPopup(view);
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(6)
                .map(aLong -> 5 - aLong)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {

                    @Override public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override public void onNext(Long o) {
                        tvCount.setText("点击复活 " + o);
                    }

                    @Override public void onError(Throwable e) {
                    }

                    @Override public void onComplete() {
                        if (cardDisplayManager != null) {
                            cardDisplayManager.setIsCanRevive(false);
                            cardDisplayManager.propCountdownOver();
                        }
                        if (popView != null) {
                            dismiss();
                        }
                    }
                });
    }

    private void setPopup(View view) {
        int[] location = new int[2];
        view.getLocationInWindow(location);
        int x = location[0];
        int y = location[1];


        //适配底部虚拟按键
        int hh = DensityUtil.dp2px(10);
        //如果屏幕高度是2030且有虚拟键，则不执行下面操作
        if (CommonUtil.isNavigationBarShow((Activity) mContext)&& CommonUtil.getScreenWidthHeight((Activity) mContext)[1]!=2030) {
            hh = DensityUtil.dp2px(10) + CommonUtil.navigationBarHeight((Activity) mContext);
        }
        LogUtils.e("widht:" + CommonUtil.getScreenWidthHeight((Activity) mContext)[0] + "height:"
                + CommonUtil.getScreenWidthHeight((Activity) mContext)[1] + "-----hh:" + hh
                + "---viewheght:" + CommonUtil.getViewHeight(view));
        this.showAtLocation(view, Gravity.BOTTOM | Gravity.LEFT,
                x - CommonUtil.getViewWidth(view) / 2 + DensityUtil.dp2px(5),
                CommonUtil.getViewHeight(view) + hh);
    }

    @Override public void dismiss() {
        super.dismiss();
        if (disposable != null) {
            disposable.dispose();
        }
    }
}
